package ctp.ykit.com.yctp;
/*
*   author：ykit
*   FAQ by email:cn0512@126.com
* */
import android.graphics.ImageFormat;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.sfit.ctp.thostmduserapi.CThostFtdcDepthMarketDataField;
import com.sfit.ctp.thostmduserapi.CThostFtdcForQuoteRspField;
import com.sfit.ctp.thostmduserapi.CThostFtdcMdApi;
import com.sfit.ctp.thostmduserapi.CThostFtdcReqUserLoginField;
import com.sfit.ctp.thostmduserapi.CThostFtdcRspInfoField;
import com.sfit.ctp.thostmduserapi.CThostFtdcRspUserLoginField;
import com.sfit.ctp.thostmduserapi.CThostFtdcSpecificInstrumentField;
import com.sfit.ctp.thostmduserapi.CThostFtdcUserLogoutField;

public class MainActivity extends AppCompatActivity implements IQuoteEvent{

    public static CQuote quote;
    public static CThostFtdcMdApi api;
    public static int iQuoteReq;
    public static String _brokerid="9999";
    public static String _userid="xx";
    public  static String _pwd="xx";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        //system tmp file
        String tmpFile = this.getCacheDir().toString();
        //load ctp
        quote = new CQuote();
        quote.SetInterFace(this);
        api = CThostFtdcMdApi.CreateFtdcMdApi(tmpFile);
        api.RegisterSpi(quote);
        api.RegisterFront("tcp://180.168.146.187:10010");
        //api.RegisterFront("tcp://218.202.237.33:10012");
        //api.RegisterFensUserInfo();
        api.Init();
    }

    static
    {
        System.loadLibrary("thostmduserapi");
        System.loadLibrary("thostmduserapi_wrap");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void OnFrontConnected() {
        Log.i("YCTP","OnFrontConnected");
        CThostFtdcReqUserLoginField stLoginField = new CThostFtdcReqUserLoginField();
        stLoginField.setBrokerID(_brokerid);
        stLoginField.setUserID(_userid);
        stLoginField.setPassword(_pwd);
        api.ReqUserLogin(stLoginField,++iQuoteReq);
    }

    public void OnFrontDisconnected(int nReason) {
        Log.i("YCTP","OnFrontDisconnected");
    }

    public void OnHeartBeatWarning(int nTimeLapse) {
        Log.i("YCTP","OnHeartBeatWarning");
    }

    public void OnRspUserLogin(CThostFtdcRspUserLoginField pRspUserLogin, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
        Log.i("YCTP","OnRspUserLogin:"+pRspInfo.getErrorID());
        //if (pRspInfo.getErrorID() == 0)
       // {
            //发起订阅
            String ins[]={"ag1612"};
            api.SubscribeMarketData(ins,1);
       // }
    }

    public void OnRspUserLogout(CThostFtdcUserLogoutField pUserLogout, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
        Log.i("YCTP","OnRspUserLogout");
    }

    public void OnRspError(CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
        Log.i("YCTP","OnRspError:"+pRspInfo.getErrorID()+","+pRspInfo.getErrorMsg());
    }

    public void OnRspSubMarketData(CThostFtdcSpecificInstrumentField pSpecificInstrument, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
        Log.i("YCTP","OnRspSubMarketData");
    }

    public void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField pSpecificInstrument, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
        Log.i("YCTP","OnRspUnSubMarketData");
    }

    public void OnRspSubForQuoteRsp(CThostFtdcSpecificInstrumentField pSpecificInstrument, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
        Log.i("YCTP","OnRspSubForQuoteRsp");
    }

    public void OnRspUnSubForQuoteRsp(CThostFtdcSpecificInstrumentField pSpecificInstrument, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
        Log.i("YCTP","OnRspUnSubForQuoteRsp");
    }

    public void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField pDepthMarketData) {
        Log.i("YCTP","OnRtnDepthMarketData");
        String msg = pDepthMarketData.getInstrumentID()+","+pDepthMarketData.getLastPrice();
        Log.i("YCTP",msg);
    }

    public void OnRtnForQuoteRsp(CThostFtdcForQuoteRspField pForQuoteRsp) {
        Log.i("YCTP","OnRtnForQuoteRsp");
    }
}
